#!/usr/bin/env python
#-*-coding:utf-8-*-
# ---------------------------------
# version:  1.0
#
# create-time:      <2008/12/03 00:09:05>
# last-update-time: <halida 12/19/2008 14:26:48>
# ---------------------------------
# module : qt lib

from lib import *
from datetime import *

from PyQt4.QtCore import *
from PyQt4.QtGui import *


#app
app = QApplication(sys.argv)

#signal and slot
def conn(sender,sendEvent,reciver,reciveEvent=None):
    #send event to a function
    if reciveEvent==None:
        sender.connect(sender,SIGNAL(sendEvent),
                       reciver)
        return
    #send event to a reciver event
    else:
        sender.connect(sender,SIGNAL(sendEvent),
                       reciver,SLOT(reciveEvent))

def emit(sender,event,*args):
    sender.emit(SIGNAL(event),*args)

#tools
def toDatetime(d):
    """QDatetime to Datetime"""
    return datetime.strptime(
        unicode(d.toString(Qt.ISODate)),
        "%Y-%m-%dT%H:%M:%S")

    
def getDay(parent):
    """open a calendar window,user pick a day."""

    class Calendar(QDialog):        
        returnDatetime = None
        def __init__(self,*args):
            QDialog.__init__(self,*args)
            self.calendar = QCalendarWidget(self)
            #layout
            l = QHBoxLayout(self)
            l.addWidget(self.calendar)
            conn(self.calendar,"clicked(const QDate &)",self.returnDate)
            #conn(self,"")
            self.exec_()

        def returnDate(self,date):
            datetime = QDateTime()
            datetime.setDate(date)
            datetime = toDatetime(datetime)
            logging.info("return date:%s",unicode(datetime))
            Calendar.returnDatetime = datetime
            self.close()

    c = Calendar()
    return Calendar.returnDatetime
    
